package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.activity.MyUploadActivity.TeacherPrivateAdapter;
import com.ycsj.goldmedalnewconcept.activity.TeacherFansiListActivity.ViewHolder;
import com.ycsj.goldmedalnewconcept.bean.DelVideoRowid;
import com.ycsj.goldmedalnewconcept.bean.TeacherVideoArray;
import com.ycsj.goldmedalnewconcept.bean.TeacherVideoArrayResponse;
import com.ycsj.goldmedalnewconcept.bean.TeacherVideoInfo;
import com.ycsj.goldmedalnewconcept.bean.TeacherVideoResponse;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

// 教师风采
public class TeacherShowActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private ImageView iv_upload;
	private GridView gv_teachershow;
	private TextView tv_array;
	private RadioButton rb_shaike;
	private RadioButton rb_pingke;
	private PopupWindow mPopWindow;
	private RelativeLayout rl_array;
	private String array = "0";
	private String role;
	private TeacherShowAdapter mAdapter;
	private TeacherShowArrayAdapter mAdapter1;
	private List<TeacherVideoInfo> morenlist;
	private List<TeacherVideoArray> playlist;
	private String account;
	private String vtype;
	private TextView tv_videonull;
	private String userid;
	private Intent intent;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_teachershow);
		intent = getIntent();
		userid = intent.getStringExtra("user_id");
		role = SPUtil.getString(this, "role", "");

		if ("3".equals(role)) {
			account = userid;
		} else if ("2".equals(role)) {
			account = SPUtil.getString(this, "account", "");
		} else if ("1".equals(role)) {
			account = SPUtil.getString(this, "account", "");
		}

		morenlist = new ArrayList<TeacherVideoInfo>();
		playlist = new ArrayList<TeacherVideoArray>();
		vtype = "0";

		initView();
		setOnListener();
		initData();

		rb_shaike.setChecked(true);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		initData();
		super.onResume();
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_upload = (ImageView) findViewById(R.id.iv_upload);
		gv_teachershow = (GridView) findViewById(R.id.gv_teachershow);
		tv_array = (TextView) findViewById(R.id.tv_array);
		tv_videonull = (TextView) findViewById(R.id.tv_videonull);
		rb_shaike = (RadioButton) findViewById(R.id.rb_shaike);
		rb_pingke = (RadioButton) findViewById(R.id.rb_pingke);
		rl_array = (RelativeLayout) findViewById(R.id.rl_array);
	}

	private void initData() {
		if ("3".equals(role)) {

			OkHttpClient mOkHttpClient = new OkHttpClient();
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "25");
			builder.add("c", account);
			builder.add("role", role);
			builder.add("teacherid", account);
			builder.add("vtype", vtype);
			Request request = new Request.Builder()
					.url(Constant.SERVER_LOC + "ManageMsg")
					.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(new Callback() {

				@Override
				public void onFailure(Request arg0, IOException arg1) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
									.show();
						}
					});
				}

				@Override
				public void onResponse(Response arg0) throws IOException {
					// 解析Json数据，保存登录状态及身份信息

					Gson gson = new Gson();

					String json = arg0.body().string();
					TeacherVideoResponse fromJson = gson.fromJson(json,
							TeacherVideoResponse.class);

					TeacherVideoArrayResponse fromJson2 = gson.fromJson(json,
							TeacherVideoArrayResponse.class);

					if (fromJson != null) {
						if ("-1".equals(fromJson.state)
								|| "0".equals(fromJson.state)) {
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									// 没有数据的情况就隐藏GridView显示没有数据的TextView
									tv_videonull
											.setVisibility(tv_videonull.VISIBLE);
									gv_teachershow.setVisibility(View.GONE);
								}
							});
						} else if ("1".equals(fromJson.state)) {
							morenlist = fromJson.list;

							playlist = fromJson2.list;
							Collections.sort(playlist);
							mAdapter = new TeacherShowAdapter(morenlist);
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									tv_videonull
											.setVisibility(tv_videonull.GONE);
									gv_teachershow.setVisibility(View.VISIBLE);
									gv_teachershow.setAdapter(mAdapter);
									gv_teachershow
											.setOnItemClickListener(new OnItemClickListener() {

												@Override
												public void onItemClick(
														AdapterView<?> parent,
														View view,
														int position, long id) {
													Intent intent = new Intent(
															TeacherShowActivity.this,
															VideoWebActivity.class);
													if ("2".equals(array)) {
														intent.putExtra(
																"row_id",
																playlist.get(position).row_id);
														intent.putExtra(
																"url",
																playlist.get(position).url);
														intent.putExtra("title",playlist.get(position).title);
													} else {
														intent.putExtra(
																"row_id",
																morenlist
																		.get(position).row_id);
														intent.putExtra(
																"url",
																morenlist
																		.get(position).url);
														intent.putExtra("title",morenlist.get(position).title);
													}
													
													
													startActivity(intent);
												}
											});

								}
							});
						}
					}
				}
			});
		} else {

			OkHttpClient mOkHttpClient = new OkHttpClient();
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "26");
			builder.add("c", account);
			builder.add("role", role);
			builder.add("vtype", vtype);
			Request request = new Request.Builder()
					.url(Constant.SERVER_LOC + "ManageMsg")
					.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(new Callback() {

				@Override
				public void onFailure(Request arg0, IOException arg1) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
									.show();
						}
					});
				}

				@Override
				public void onResponse(Response arg0) throws IOException {
					// 解析Json数据，保存登录状态及身份信息

					Gson gson = new Gson();

					String json = arg0.body().string();
					Log.e("test", json);
					TeacherVideoResponse fromJson = gson.fromJson(json,
							TeacherVideoResponse.class);

					TeacherVideoArrayResponse fromJson2 = gson.fromJson(json,
							TeacherVideoArrayResponse.class);

					if (fromJson != null) {
						if ("-1".equals(fromJson.state)
								|| "0".equals(fromJson.state)) {
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									tv_videonull
											.setVisibility(tv_videonull.VISIBLE);
									gv_teachershow
											.setVisibility(gv_teachershow.GONE);
								}
							});
						} else if ("1".equals(fromJson.state)) {
							morenlist = fromJson.list;

							playlist = fromJson2.list;
							Collections.sort(playlist);

							mAdapter = new TeacherShowAdapter(morenlist);
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									tv_videonull
											.setVisibility(tv_videonull.GONE);
									gv_teachershow
											.setVisibility(gv_teachershow.VISIBLE);
									gv_teachershow.setAdapter(mAdapter);
									gv_teachershow
											.setOnItemClickListener(new OnItemClickListener() {

												@Override
												public void onItemClick(
														AdapterView<?> parent,
														View view,
														int position, long id) {
													Intent intent = new Intent(
															TeacherShowActivity.this,
															VideoWebActivity.class);
													if ("2".equals(array)) {
														intent.putExtra(
																"row_id",
																playlist.get(position).row_id);
														intent.putExtra(
																"url",
																playlist.get(position).url);
														intent.putExtra("title",playlist.get(position).title);
													} else {
														intent.putExtra(
																"row_id",
																morenlist
																		.get(position).row_id);
														intent.putExtra(
																"url",
																morenlist
																		.get(position).url);
														intent.putExtra("title",morenlist.get(position).title);
													}
													
													startActivity(intent);
												}
											});
								}
							});
						}
					}
				}
			});
		}
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
		tv_array.setOnClickListener(this);
		rb_shaike.setOnClickListener(this);
		rb_pingke.setOnClickListener(this);
		if ("2".equals(role)) {
			iv_upload.setVisibility(iv_upload.VISIBLE);
			iv_upload.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.iv_upload:
			Intent intent = new Intent(this, MyUploadActivity.class);
			startActivity(intent);
			break;
		case R.id.rb_shaike: // 晒课
			vtype = "0";
			initData();
			break;
		case R.id.rb_pingke: // 评课
			vtype = "1";
			initData();
			break;
		case R.id.tv_array:// 排序方式
			Popwindow();
			break;
		case R.id.rb_default:// 默认排序
			array = "0";
			mPopWindow.dismiss();

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					tv_array.setText("默认排序");
					mAdapter = new TeacherShowAdapter(morenlist);
					gv_teachershow.setAdapter(mAdapter);
					gv_teachershow
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> parent,
										View view, int position, long id) {
									Intent intent = new Intent(
											TeacherShowActivity.this,
											VideoWebActivity.class);
									if ("2".equals(array)) {
										intent.putExtra("row_id",
												playlist.get(position).row_id);
										intent.putExtra("url",
												playlist.get(position).url);
										intent.putExtra("title",playlist.get(position).title);
									} else {
										intent.putExtra("row_id",
												morenlist.get(position).row_id);
										intent.putExtra("url",
												morenlist.get(position).url);
										intent.putExtra("title",morenlist.get(position).title);
									}
									
									startActivity(intent);
								}
							});
				}
			});
			break;

		case R.id.rb_new:// 最新排序
			array = "1";
			mPopWindow.dismiss();

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					tv_array.setText("人气指数");
					mAdapter = new TeacherShowAdapter(morenlist);
					gv_teachershow.setAdapter(mAdapter);
					gv_teachershow
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> parent,
										View view, int position, long id) {
									Intent intent = new Intent(
											TeacherShowActivity.this,
											VideoWebActivity.class);
									if ("2".equals(array)) {
										intent.putExtra("row_id",
												playlist.get(position).row_id);
										intent.putExtra("url",
												playlist.get(position).url);
										intent.putExtra("title",playlist.get(position).title);
									} else {
										intent.putExtra("row_id",
												morenlist.get(position).row_id);
										intent.putExtra("url",
												morenlist.get(position).url);
										intent.putExtra("title",morenlist.get(position).title);
									}
									
									startActivity(intent);
								}
							});
				}
			});
			break;

		case R.id.rb_total:// 播放最多排序
			array = "2";
			mPopWindow.dismiss();
			Collections.sort(playlist);

			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					tv_array.setText("播放最多");

					mAdapter1 = new TeacherShowArrayAdapter(playlist);
					gv_teachershow.setAdapter(mAdapter1);
					gv_teachershow
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> parent,
										View view, int position, long id) {
									Intent intent = new Intent(
											TeacherShowActivity.this,
											VideoWebActivity.class);
									if ("2".equals(array)) {
										intent.putExtra("row_id",
												playlist.get(position).row_id);
										intent.putExtra("url",
												playlist.get(position).url);
										intent.putExtra("title",playlist.get(position).title);
									} else {
										intent.putExtra("row_id",
												morenlist.get(position).row_id);
										intent.putExtra("url",
												morenlist.get(position).url);
										intent.putExtra("title",morenlist.get(position).title);
									}
									
									startActivity(intent);
								}
							});
				}
			});
			break;
		}
	}

	class ViewHolder {
		public TextView tv_name;
		public ImageView iv_image;
		public TextView tv_time;
		public TextView tv_score;
	}

	class TeacherShowAdapter extends BaseAdapter {
		List<TeacherVideoInfo> list = new ArrayList<TeacherVideoInfo>();

		public TeacherShowAdapter(List<TeacherVideoInfo> list) {
			this.list = list;
		}

		public int getCount() {

			return list.size();
		}

		public Object getItem(int position) {

			return null;
		}

		public long getItemId(int position) {

			return 0;
		}

		ViewHolder vh = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.gv_teachershow_item, null);
				vh = new ViewHolder();

				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
			vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			vh.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			vh.tv_score = (TextView) convertView.findViewById(R.id.tv_score);

			Picasso.with(TeacherShowActivity.this)
					.load(list.get(position).picture_url).into(vh.iv_image);
			vh.tv_name.setText(list.get(position).title);
			vh.tv_time.setText(list.get(position).create_date.substring(0, 10)
					+ "");
			vh.tv_score.setText(list.get(position).play_times + "");

			return convertView;
		}

	}

	class TeacherShowArrayAdapter extends BaseAdapter {
		List<TeacherVideoArray> list = new ArrayList<TeacherVideoArray>();

		public TeacherShowArrayAdapter(List<TeacherVideoArray> list) {
			this.list = list;
		}

		public int getCount() {

			return list.size();
		}

		public Object getItem(int position) {

			return null;
		}

		public long getItemId(int position) {

			return 0;
		}

		ViewHolder vh = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.gv_teachershow_item, null);
				vh = new ViewHolder();

				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
			vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			vh.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			vh.tv_score = (TextView) convertView.findViewById(R.id.tv_score);

			Picasso.with(TeacherShowActivity.this)
					.load(list.get(position).picture_url).into(vh.iv_image);
			vh.tv_name.setText(list.get(position).title);
			vh.tv_time.setText(list.get(position).create_date + "");
			vh.tv_score.setText(list.get(position).play_times + "");

			return convertView;
		}

	}

	private void Popwindow() {

		View contentView = LayoutInflater.from(this).inflate(
				R.layout.popup_array, null);
		mPopWindow = new PopupWindow(contentView,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopWindow.setContentView(contentView);
		// 设置各个控件的点击响应
		RadioButton rb_default = (RadioButton) contentView
				.findViewById(R.id.rb_default);
		RadioButton rb_new = (RadioButton) contentView
				.findViewById(R.id.rb_new);
		RadioButton rb_total = (RadioButton) contentView
				.findViewById(R.id.rb_total);
		rb_default.setOnClickListener(this);
		rb_new.setOnClickListener(this);
		rb_total.setOnClickListener(this);

		mPopWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopWindow.setOutsideTouchable(true);
		mPopWindow.showAsDropDown(rl_array);
		if ("0".equals(array)) {
			rb_default.setChecked(true);
		} else if ("1".equals(array)) {
			rb_new.setChecked(true);
		} else if ("2".equals(array)) {
			rb_total.setChecked(true);
		}
	}
}
